<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet version="1.0"
		xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
		xmlns:cat="http://www.unidata.ucar.edu/namespaces/thredds/InvCatalog/v1.0"
		xmlns:dc="http://purl.org/dc/elements/1.1/"
		xmlns:dct="http://purl.org/dc/terms/"
		xmlns:ical="http://www.w3.org/2002/12/cal/ical#"
		xmlns:foaf="http://xmlns.com/foaf/spec/"
		xmlns:dclite4g="http://xmlns.com/2008/dclite4g#"
		xmlns:t2ext="http://catalog.terradue.com/schemas/thredds.xsl#">
  
  <xsl:output method="xml" indent="yes" />
 
  <!-- setting HTTP base URL globally if available --> 
<xsl:variable name="serviceBase">
  <xsl:choose>      
    <xsl:when test="contains('Compound',cat:catalog/cat:service/@serviceType)">
      <xsl:if test="cat:catalog/cat:service/cat:service">
           <xsl:for-each select="cat:catalog/cat:service/cat:service">
                <xsl:if test="contains('HTTPServer',@serviceType)">
                     <xsl:value-of select="@base"/>
                </xsl:if>
           </xsl:for-each>
           <xsl:for-each select="cat:catalog/cat:service/cat:service">
                  <!-- will set if as yet unset -->
                   <xsl:value-of select="cat:catalog/cat:service/cat:service/@base"/>
           </xsl:for-each>

      </xsl:if>
    </xsl:when>
    <xsl:otherwise>
        <xsl:value-of select="cat:catalog/cat:service/@base"/>
    </xsl:otherwise>
  </xsl:choose>  
  </xsl:variable>
  <!-- write out the RDF header and footer -->
  <xsl:template match="cat:catalog">
    <rdf:RDF
       xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
      
      <xsl:if test="cat:service/@base">
	<xsl:attribute name="xml:base">
	  <xsl:if test="starts-with(cat:service/@base, '/')">
	    <xsl:value-of select="$base_uri" />
	  </xsl:if>
	  <xsl:value-of select="cat:service/@base" />
	</xsl:attribute>
      </xsl:if>

      <!--
	  Process the DataProduct a.k.a. Series. With THREDDS datasets,
	  we put the majority of the metadata such as contact information
	  and keywords here.
	-->
      <dclite4g:Series>
	<xsl:attribute name="rdf:about">
	  <xsl:value-of select="$product_uri" />
	</xsl:attribute>
	<dc:title>
	  <xsl:value-of select="cat:dataset/cat:metadata/cat:documentation[@type=&quot;summary&quot;]" />
	</dc:title>
	<dc:contact>
	  <xsl:attribute name="rdf:resource">
	    <xsl:value-of select="cat:dataset/cat:metadata/cat:creator/cat:contact/@url" />
          </xsl:attribute>
        </dc:contact>
        <xsl:if test="cat:dataset/cat:metadata/@inherited">
     <!-- if spatial coverage is inherited, apply only to Series --> 
          <xsl:variable name="inherit" select="cat:dataset/cat:metadata/@inherited"/>
          <xsl:if test="contains('true',$inherit)">
              <dct:spatial><xsl:apply-templates select="cat:dataset/cat:metadata/cat:geospatialCoverage" /></dct:spatial>
          </xsl:if>
        </xsl:if>
      </dclite4g:Series>

      <!--
	  Make sure to embed the contact information that we have for this
	  data series. Not all information may be available, and we use
	  the foaf namespace here.
	-->
      <foaf:Person>
	<xsl:attribute name="rdf:about">
	  <xsl:value-of select="cat:dataset/cat:metadata/cat:creator/cat:contact/@url" />
	</xsl:attribute>
	<xsl:if test="cat:dataset/cat:metadata/cat:creator/cat:name">
	  <foaf:name>
	    <xsl:value-of select="cat:dataset/cat:metadata/cat:creator/cat:name" />
	  </foaf:name>
	</xsl:if>
	<xsl:if test="cat:dataset/cat:metadata/cat:creator/cat:contact/@email">
	  <foaf:mbox>
	    <xsl:value-of select="cat:dataset/cat:metadata/cat:creator/cat:contact/@email" />
	  </foaf:mbox>
	</xsl:if>
      </foaf:Person>

      <!-- 
	   Here we descend down the hierarchy of datasets embedded in the
	   THREDDS summary.
	-->
      <xsl:call-template name="process_dataset">
	<xsl:with-param name="product">
	  <xsl:value-of select="$product_uri" />
	</xsl:with-param>
	<xsl:with-param name="dataset" select="cat:dataset" />
	<xsl:with-param name="format" 
			select="cat:dataset/cat:metadata/cat:dataFormat" />
	<xsl:with-param name="time_start" 
			select="cat:dataset/cat:metadata/cat:timeCoverage/cat:start" />
	<xsl:with-param name="time_end" 
			select="cat:dataset/cat:metadata/cat:timeCoverage/cat:end" />
	<xsl:with-param name="geo_cover">
	  <xsl:apply-templates select="cat:dataset/cat:metadata/cat:geospatialCoverage" />
	</xsl:with-param>
      </xsl:call-template>
    </rdf:RDF>
  </xsl:template>

  <!--
      This template functions like a recursive subroutine for descending
      the THREDDS datasets.
    -->
  <xsl:template name="process_dataset">
    <xsl:param name="product" />
    <xsl:param name="dataset" />
    <xsl:param name="format" />
    <xsl:param name="time_start" />
    <xsl:param name="time_end" />
    <xsl:param name="geo_cover" />

    <!--
	THREDDS makes extensive use of nested datasets as containers. We only
	consider datasets with an URL to be genuine datasets. If the current
	node is one, we output its RDF representation.
      -->
    <xsl:if test="$dataset/@urlPath">
      <dclite4g:DataSet>
	<xsl:attribute name="about">
          <xsl:value-of select="$base_uri"/>
          <xsl:copy-of select="$serviceBase"/>
	  <xsl:value-of select="$dataset/@urlPath" />
	</xsl:attribute>
	<dclite4g:series>
	  <xsl:attribute name="rdf:resource">
	    <xsl:value-of select="$product" />
	  </xsl:attribute>
	</dclite4g:series>
	<dc:identifier><xsl:value-of select="./@ID" /></dc:identifier>
	<dclite4g:fileFormat>
	  <xsl:value-of select="$format" />
	</dclite4g:fileFormat>
	<ical:dtstart><xsl:value-of select="$time_start" /></ical:dtstart>
	<ical:dtend>
	  <!--
	      Pedro's Space-Time Anomaly

	      Special rule for correcting the end time in certain cases.
	      See the file transform.py for details.
	    -->
	  <xsl:value-of select="t2ext:addOneDay(string($time_start), string($time_end))" />
	</ical:dtend>
       
        <!-- only copy spatial coverage if "inherited" is false or undefined (nd thus by default false -->
        <xsl:if test="/cat:dataset/cat:metadata/@inherited">
     <!-- if spatial coverage is inherited, apply only to Series -->
          <xsl:variable name="inherit" select="cat:dataset/cat:metadata/@inherited"/>
          <xsl:if test="contains('false',$inherit)">
	    <dct:spatial><xsl:value-of select="$geo_cover" /></dct:spatial>
          </xsl:if>
        </xsl:if>
        <xsl:if test="not(/cat:catalog/cat:dataset/cat:metadata/@inherited)">
        <dct:spatial><xsl:value-of select="$geo_cover" /></dct:spatial>
        </xsl:if>
      </dclite4g:DataSet>
    </xsl:if>

    <!--
	Iterate through datasets contained in the present one.
      -->
    <xsl:for-each select="cat:dataset">
      <xsl:call-template name="process_dataset">
	<xsl:with-param name="product" select="$product" />
	<xsl:with-param name="dataset" select="." />
	<xsl:with-param name="format" select="$format" />
	<xsl:with-param name="time_start" select="cat:timeCoverage/cat:start" />
	<xsl:with-param name="time_end" select="cat:timeCoverage/cat:end" />
	<xsl:with-param name="geo_cover">
	  <xsl:choose>
	    <xsl:when test="cat:geospatialCoverage">
	      <xsl:apply-templates select="cat:geospatialCoverage" />
	    </xsl:when>
	    <xsl:otherwise>
	      <xsl:value-of select="$geo_cover" />
	    </xsl:otherwise>
	  </xsl:choose>
	</xsl:with-param>
      </xsl:call-template>
    </xsl:for-each>

  </xsl:template>

  <!-- 
       Special template routine to transform THREDDS geospatial
       coverage tag to our own convention.
    -->
  <xsl:template match="cat:geospatialCoverage">
    <xsl:variable name="lat_start" select="cat:northsouth/cat:start" />
    <xsl:variable name="lat_size" select="cat:northsouth/cat:size" />
    <xsl:variable name="lon_start" select="cat:eastwest/cat:start" />
    <xsl:variable name="lon_size" select="cat:eastwest/cat:size" />
    <xsl:text>POLYGON ((</xsl:text>
    <xsl:value-of select="$lon_start" />
    <xsl:text> </xsl:text>
    <xsl:value-of select="$lat_start" />
    <xsl:text>, </xsl:text>
    <xsl:value-of select="$lon_start" />
    <xsl:text> </xsl:text>
    <xsl:value-of select="number($lat_start) + number($lat_size)" />
    <xsl:text>, </xsl:text>
    <xsl:value-of select="number($lon_start) + number($lon_size)" />
    <xsl:text> </xsl:text>
    <xsl:value-of select="number($lat_start) + number($lat_size)" />
    <xsl:text>, </xsl:text>
    <xsl:value-of select="number($lon_start) + number($lon_size)" />
    <xsl:text> </xsl:text>
    <xsl:value-of select="$lat_start" />
    <xsl:text>, </xsl:text>
    <xsl:value-of select="$lon_start" />
    <xsl:text> </xsl:text>
    <xsl:value-of select="$lat_start" />
    <xsl:text>))</xsl:text>
  </xsl:template>

</xsl:stylesheet>
